package com.prj.ufdm.core.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

/**
 * 文件处理工具类
 * @author Administrator
 *
 */
public class UfdmFileUtil {

	public static void main(String[] args) throws Exception {
		
		File file = new File("D:\\ziptest\\GRXX_C41010_20190104_ADD_0001.zip");
//		splitFile(file,1024*1024,"part");
		
		combineFile(file,"part");
		
	}
	
	/**
	 * 文件写入
	 * @param writeFile
	 * @param content
	 */
	public static void writeFile(File writeFile,String content) {
		OutputStreamWriter writer = null;
        BufferedWriter bw = null;
        try {
            OutputStream os = new FileOutputStream(writeFile);
            writer = new OutputStreamWriter(os);
            bw = new BufferedWriter(writer);
            bw.write(content);
            bw.flush();
            if(writeFile.exists()){
            	writeFile.delete();
            }
        } catch (Exception e) { 
            e.printStackTrace();
        } finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
	}
	
	
	/**
	 * 分割文件（生成格式：源文件.000001.后缀名称）
	 * @param file
	 * @param partSize
	 * @throws Exception
	 */
	public static void splitFile(File srcFile,int partSize,String suffix) throws Exception {
		
		if(partSize <= 0) {
			partSize = 1024*1024; //默认1M
		}
		if(!srcFile.isFile()) {
			throw new Exception("file not exists:"+srcFile.getAbsolutePath());
		}
		byte [] buf = new byte[1024];
		FileInputStream fis = new FileInputStream(srcFile);
		FileOutputStream fos = null;
		try 
		{
			String filename = srcFile.getAbsolutePath();
			int readlength = -1;
			int currPosit = 0;
			int partNum = 1 ;
			int lastNum = 0;
			while((readlength = fis.read(buf,0,buf.length)) > 0) {
				if(partNum != lastNum) {
					if(fos!=null) {
						fos.close();
						fos = null;
					}
					fos = new FileOutputStream(new File(filename+"."+UfdmStringUtil.fillfulString(""+partNum, 6)+"."+suffix));
					lastNum = partNum;
				}
				fos.write(buf,0,readlength);
				fos.flush();
				currPosit += readlength;
				if( currPosit >= partSize * partNum )  {
					partNum++;
				}
			}
		}catch(Exception er) {
			er.printStackTrace();
		}
		finally {
			if(fos!=null){
			    fos.close();
			    fos = null;
			}
			fis.close();
		}
    }
	
	/**
	 * 合并文件
	 * @param targetFile
	 * @param suffix
	 * @throws Exception
	 */
	public static void combineFile(File targetFile,String suffix) throws Exception {
		
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try
		{
			String filename = targetFile.getAbsolutePath();
			File fileout = new File(filename);
	        fos = new FileOutputStream(fileout);
	        int partNum = 1;
	        File inputFile = null;
			byte [] buf = new byte[1024*10];
			while(true) {
				if(fis!=null) {
					fis.close();
					fis = null;
				}
				inputFile = new File(filename+"."+UfdmStringUtil.fillfulString(""+partNum, 6)+"."+suffix);
				if(!inputFile.isFile()) {
					break;
				}
				fis = new FileInputStream(inputFile);
				int readsize = 0;
				while((readsize = fis.read(buf,0,buf.length))>0) {
					fos.write(buf, 0, readsize);
					fos.flush();
				}
				partNum ++ ;
			}
		}
		catch(Exception er) {
			er.printStackTrace();
		}
		finally {
			if(fis!=null) {
				fis.close();
				fis = null;
			}
			if(fos!=null) {
				fos.close();
				fos = null;
			}
		}
    }
}
